clear all
set mem 300m

// Uncomment the next line and set the path to the directory containing this do file (without trailing slash).
*global repos "path/to/here"

use "$repos/data/issp/1996_RoleOfGovernment_III/ZA2900_F1.dta"

gen     ctry = ""
replace ctry = "Australia" if v3 == 1
replace ctry = "Germany" if v3 == 2
replace ctry = "UK" if v3 == 4 | v3 == 5
replace ctry = "USA" if v3 == 6
replace ctry = "Austria" if v3 == 7
replace ctry = "Hungary" if v3 == 8
replace ctry = "Italy" if v3 == 9
replace ctry = "Ireland" if v3 == 10
replace ctry = "Netherlands" if v3 == 11
replace ctry = "Norway" if v3 == 12
replace ctry = "Sweden" if v3 == 13
replace ctry = "CzechRepublic" if v3 == 14
replace ctry = "Slovenia" if v3 == 15
replace ctry = "Poland" if v3 == 16
replace ctry = "Bulgaria" if v3 == 17
replace ctry = "Russia" if v3 == 18
replace ctry = "NewZealand" if v3 == 19
replace ctry = "Canada" if v3 == 20
replace ctry = "Philippines" if v3 == 21
replace ctry = "Japan" if v3 == 24
replace ctry = "Spain" if v3 == 25
replace ctry = "Latvia" if v3 == 26
replace ctry = "France" if v3 == 27
replace ctry = "Cyprus" if v3 == 28
replace ctry = "Switzerland" if v3 == 30

gen     ctry_Australia = 1 if v3 == 1
replace ctry_Australia = 0 if ctry_Australia == .
gen     ctry_Germany = 1 if v3 == 2
replace ctry_Germany = 0 if ctry_Germany == .
gen     ctry_Britain = 1 if v3 == 4
replace ctry_Britain = 0 if ctry_Britain == .
gen     ctry_NorthernIreland = 1 if v3 == 5
replace ctry_NorthernIreland = 0 if ctry_NorthernIreland == .
gen     ctry_UK = 1 if ctry_Britain == 1 | ctry_NorthernIreland == 1
replace ctry_UK = 0 if ctry_UK == .
gen     ctry_USA = 1 if v3 == 6
replace ctry_USA = 0 if ctry_USA == .
gen     ctry_Austria = 1 if v3 == 7
replace ctry_Austria = 0 if ctry_Austria == .
gen     ctry_Hungary = 1 if v3 == 8
replace ctry_Hungary = 0 if ctry_Hungary == .
gen     ctry_Italy = 1 if v3 == 9
replace ctry_Italy = 0 if ctry_Italy == .
gen     ctry_Ireland = 1 if v3 == 10
replace ctry_Ireland = 0 if ctry_Ireland == .
gen     ctry_Netherlands = 1 if v3 == 11
replace ctry_Netherlands = 0 if ctry_Netherlands == .
gen     ctry_Norway = 1 if v3 == 12
replace ctry_Norway = 0 if ctry_Norway == .
gen     ctry_Sweden = 1 if v3 == 13
replace ctry_Sweden = 0 if ctry_Sweden == .
gen     ctry_CzechRepublic = 1 if v3 == 14
replace ctry_CzechRepublic = 0 if ctry_CzechRepublic == .
gen     ctry_Slovenia = 1 if v3 == 15
replace ctry_Slovenia = 0 if ctry_Slovenia == .
gen     ctry_Poland = 1 if v3 == 16
replace ctry_Poland = 0 if ctry_Poland == .
gen     ctry_Bulgaria = 1 if v3 == 17
replace ctry_Bulgaria = 0 if ctry_Bulgaria == .
gen     ctry_Russia = 1 if v3 == 18
replace ctry_Russia = 0 if ctry_Russia == .
gen     ctry_NewZealand = 1 if v3 == 19
replace ctry_NewZealand = 0 if ctry_NewZealand == .
gen     ctry_Canada = 1 if v3 == 20
replace ctry_Canada = 0 if ctry_Canada == .
gen     ctry_Philippines = 1 if v3 == 21
replace ctry_Philippines = 0 if ctry_Philippines == .
/// Miss out Israel Arabs and Jews.
gen     ctry_Japan = 1 if v3 == 24
replace ctry_Japan = 0 if ctry_Japan == .
gen     ctry_Spain = 1 if v3 == 25
replace ctry_Spain = 0 if ctry_Spain == .
gen     ctry_Latvia = 1 if v3 == 26
replace ctry_Latvia = 0 if ctry_Latvia == .
gen     ctry_France = 1 if v3 == 27
replace ctry_France = 0 if ctry_France == .
gen     ctry_Cyprus = 1 if v3 == 28
replace ctry_Cyprus = 0 if ctry_Cyprus == .
gen     ctry_Switzerland = 1 if v3 == 30
replace ctry_Switzerland = 0 if ctry_Switzerland == .

gen survey_year = .
replace survey_year = 1997 if ctry == "France"
replace survey_year = 1996 if ctry == "Germany" | ctry == "Britain" | ctry == "Ireland" | ctry == "Italy" | ctry == "Spain" | ctry == "Sweden"

// Recode priv[elec|hosp|bank] to be support for privatization rather than anti
recode v60 (1=1) (2=0), generate(privelec)
recode v61 (1=1) (2=0), generate(privhosp)
recode v62 (1=1) (2=0), generate(privbank)

// Get rid of missing incomes
gen inc = v218 if v218 < 999990
// Generate country max incomes
by v3, sort: egen inc_max = max(inc)
// Generate standardised income that places people in their within-country income distribution
gen stinc = inc/inc_max

recode v200 (1=0) (2=1), generate(female)

gen age = v201
replace age = . if age == 99

gen     relat_married = 1 if v202 == 1
replace relat_married = 0 if relat_married == . & v202 != 0 & v202 != 9
gen relat_widowed = 1 if v202 == 2
replace relat_widowed = 0 if relat_widowed == . & v202 != 0 & v202 != 9
gen relat_divorced = 1 if v202 == 3
replace relat_divorced = 0 if relat_divorced == . & v202 != 0 & v202 != 9
gen relat_separated = 1 if v202 == 4
replace relat_separated = 0 if relat_separated == . & v202 != 0 & v202 != 9
gen relat_single = 1 if v202 == 5
replace relat_single = 0 if relat_single == . & v202 != 0 & v202 != 9
gen relat_other = 1 if v202 > 5
replace relat_other = 0 if relat_other == . & v202 != 0 & v202 != 9

gen eduyears = v204
replace eduyears = . if eduyears > 90

gen     empl_fulltime = 1 if v206 == 1
replace empl_fulltime = 0 if empl_fulltime == . & v206 != 99
gen     empl_parttime = 1 if v206 == 2
replace empl_parttime = 0 if empl_parttime == . & v206 != 99
gen     empl_lesstime = 1 if v206 == 3
replace empl_lesstime = 0 if empl_lesstime == . & v206 != 99
gen     empl_family   = 1 if v206 == 4
replace empl_family   = 0 if empl_family == . & v206 != 99
gen     empl_unempl = 1 if v206 == 5
replace empl_unempl = 0 if empl_unempl == . & v206 != 99
gen     empl_student = 1 if v206 == 6
replace empl_student = 0 if empl_student == . & v206 != 99
gen     empl_retired = 1 if v206 == 7
replace empl_retired = 0 if empl_retired == . & v206 != 99
gen     empl_housewf = 1 if v206 == 8
replace empl_housewf = 0 if empl_housewf == . & v206 != 99
gen     empl_disabled = 1 if v206 == 9
replace empl_disabled = 0 if empl_disabled == . & v206 != 99
gen     empl_other = 1 if empl_fulltime == 0 & empl_parttime == 0 & empl_lesstime == 0 & empl_family == 0 & empl_unempl == 0 & empl_student == 0 & empl_retired == 0 & empl_housewf == 0 & empl_disabled == 0 & v206 != 99
replace empl_other = 0 if empl_other == .

recode v212 (1=1) (0=0)   (2/8=0) (9=.), generate(sector_govt)
recode v212 (2=1) (0/1=0) (3/8=0) (9=.), generate(sector_soe)
gen     sectorpub = 1 if sector_govt == 1 | sector_soe == 1
replace sectorpub = 0 if sectorpub == . & v212 != 9

recode v222 (1=1) (2=0) (0=0), generate(union)

gen     lrid  = v223 if v223 != 0 & v223 < 6
replace lrid  = 6-lrid
gen     lridb = v223 if v223 > 1 & v223 < 5
replace lridb = 6-lridb

gen pub_x_stinc = sectorpub*stinc
gen pub_x_female = sectorpub*female
gen soe_x_stinc = sector_soe *stinc
gen govt_x_female = sector_govt *female
gen govt_x_stinc = sector_govt*stinc

gen     eu15 = 1 if ctry == "Austria" | ctry == "France" | ctry == "Germany" | ctry == "Ireland" | ctry == "Italy" | ctry == "Netherlands" | ctry == "Spain" | ctry == "Sweden" | ctry == "UK"
replace eu15 = 0 if eu15 == .

gen     rich = 1 if eu15 | ctry == "USA" | ctry == "Australia" | ctry == "Norway" | ctry == "NewZealand" | ctry == "Canada" | ctry == "Japan" | ctry == "Cyprus" | ctry == "Switzerland"
replace rich = 0 if rich == .

svyset v325

// Taken from Visser data
gen     wcoord = .
replace wcoord = 1 if ctry == "UK"
replace wcoord = 5 if ctry == "Ireland"
replace wcoord = 4 if ctry == "Netherlands"
replace wcoord = 5 if ctry == "Belgium"
replace wcoord = 2 if ctry == "France"
replace wcoord = 3 if ctry == "Spain"
replace wcoord = 4 if ctry == "Portugal"
replace wcoord = 4 if ctry == "Germany"
replace wcoord = 4 if ctry == "Austria"
replace wcoord = 4 if ctry == "Italy"
replace wcoord = 4 if ctry == "Greece"
replace wcoord = 4 if ctry == "Finland"
replace wcoord = 3 if ctry == "Sweden"
replace wcoord = 3 if ctry == "Denmark"
replace wcoord = 5 if ctry == "Norway"

replace wcoord = 2 if ctry == "Bulgaria"
replace wcoord = 1 if ctry == "Canada"
replace wcoord = 2 if ctry == "Cyprus"
replace wcoord = 2 if ctry == "CzechRepublic"
replace wcoord = 4 if ctry == "Japan"
replace wcoord = 1 if ctry == "USA"
replace wcoord = 1 if ctry == "Poland"
replace wcoord = 1 if ctry == "NewZealand"

gen     level = .
replace level = 1 if ctry == "UK"
replace level = 4 if ctry == "Ireland"
replace level = 3 if ctry == "Netherlands"
replace level = 3 if ctry == "Belgium"
replace level = 2 if ctry == "France"
replace level = 3 if ctry == "Spain"
replace level = 4 if ctry == "Portugal"
replace level = 3 if ctry == "Germany"
replace level = 3 if ctry == "Austria"
replace level = 3 if ctry == "Italy"
replace level = 4 if ctry == "Greece"
replace level = 5 if ctry == "Finland"
replace level = 3 if ctry == "Sweden"
replace level = 3 if ctry == "Denmark"
replace level = 5 if ctry == "Norway"

replace level = 3 if ctry == "Bulgaria"
replace level = 1 if ctry == "Canada"
replace level = 2 if ctry == "Cyprus"
replace level = 2 if ctry == "CzechRepublic"
replace level = 1 if ctry == "Japan"
replace level = 1 if ctry == "USA"
replace level = 1 if ctry == "Poland"
replace level = 1 if ctry == "NewZealand"


gen     ccode2 = ""
replace ccode2 = "AU" if v3 == 1
replace ccode2 = "DE" if v3 == 2
replace ccode2 = "UK" if v3 == 4 | v3 == 5
replace ccode2 = "US" if v3 == 6
replace ccode2 = "AT" if v3 == 7
replace ccode2 = "HU" if v3 == 8
replace ccode2 = "IT" if v3 == 9
replace ccode2 = "IE" if v3 == 10
replace ccode2 = "NL" if v3 == 11
replace ccode2 = "NO" if v3 == 12
replace ccode2 = "SE" if v3 == 13
replace ccode2 = "CZ" if v3 == 14
replace ccode2 = "SI" if v3 == 15
*replace ccode2 = "Poland" if v3 == 16
*replace ccode2 = "Bulgaria" if v3 == 17
*replace ccode2 = "Russia" if v3 == 18
replace ccode2 = "NZ" if v3 == 19
replace ccode2 = "CA" if v3 == 20
*replace ccode2 = "Philippines" if v3 == 21
replace ccode2 = "JP" if v3 == 24
replace ccode2 = "ES" if v3 == 25
replace ccode2 = "Latvia" if v3 == 26
replace ccode2 = "FR" if v3 == 27
replace ccode2 = "CY" if v3 == 28
replace ccode2 = "CH" if v3 == 30


gen pub_x_wcoord = sectorpub * wcoord
gen union_x_wcoord = union*wcoord
gen pub_x_union = sectorpub * union

*gen union_x_uden = union*uden

encode ctry, generate(ccode)

gen country = ctry
gen year = 1996
replace country = "New Zealand" if ctry == "NewZealand"
replace country = "USA" if ctry == "US"
merge m:1 country year using "$repos/data/armingeon/CPDSIII1990-2009_ger.dta"
drop _merge
gen prgal = 100 - dis_gall
gen union_x_prgal = union*prgal
gen pub_x_prgal = sectorpub * prgal

drop var000*
merge m:1 country year using "$repos/data/armingeon/CPDSISPSS1960-2010UPDATE_ger.dta", keepusing(singmemd almp_pmp socexp_t_pmp)
drop _merge
// Add coding for Cyprus that isn't included otherwise - PR system
replace singmemd = 0 if country == "Cyprus"


rename country countryname
replace countryname = "FRG/Germany" if countryname == "Germany"
merge m:1 countryname year using "$repos/data/worldbank/databasepoliticalinstitutions/DPI2010_stata9.dta", keepusing(mdmh)
drop _merge
replace countryname = "Germany" if countryname == "FRG/Germany"
rename countryname country

replace mdmh = . if mdmh == -999
gen logmdmh = log(mdmh)
gen lrid_x_mdmh = lrid*mdmh
gen lrid_x_logmdmh = lrid*logmdmh
gen lridb_x_mdmh = lridb*mdmh
gen lridb_x_logmdmh = lridb*logmdmh

gen lrid_x_prgal      = lrid  * prgal
gen lridb_x_prgal     = lridb * prgal
gen lrid_x_wcoord     = lrid  * wcoord
gen lridb_x_wcoord    = lridb * wcoord
gen lrid_x_effparleg  = lrid  * effpar_leg
gen lridb_x_effparleg = lridb * effpar_leg
gen lrid_x_singmemd   = lrid  * singmemd
gen lridb_x_singmemd  = lridb * singmemd
gen lrid_x_socexp     = lrid  * socexp_t_pmp
gen lrid_x_almp       = lrid  * almp_pmp

gen prrel = -dis_rel 
gen lrid_x_prrel = lrid*prrel 
gen prabso = -dis_abso 
gen lrid_x_prabso = lrid*prabso 

// Drop the extraneous countries
keep if rich == 1

*svy: mean sectorpub, over(ccode)

////// Models start here

// The base specification to test the PR*LRId hypothesis
xtmelogit privelec sectorpub stinc eduyears age union       lrid  || ccode:
mat matNg = e(N_g)
estadd scalar N_x = matNg[1,1]
estimates store PrivElecRE1
xtmelogit privelec sectorpub stinc eduyears age union prgal lrid  lrid_x_prgal  || ccode:
mat matNg = e(N_g)
estadd scalar N_x = matNg[1,1]
estimates store PrivElecRE2
// The inferences essentially survive jackknife analysis
jackknife, cluster(ccode): xtmelogit privelec sectorpub stinc eduyears age union prgal lrid lrid_x_prgal || ccode:
mat matNg = e(N_g)
estadd scalar N_x = matNg[1,1]
estimates store PrivElecRE2Jack

// The effect seems to run directly through proportionality, not through the effective number of legislative parties
xtmelogit privelec sectorpub stinc eduyears age union prgal lrid lrid_x_prgal effpar_leg lrid_x_effparleg || ccode:
mat matNg = e(N_g)
estadd scalar N_x = matNg[1,1]
estimates store PrivElecREEffPar
// The PR effect is not running through ALMP expenditure
xtmelogit privelec sectorpub stinc eduyears age union prgal lrid lrid_x_prgal almp_pmp lrid_x_almp || ccode:
mat matNg = e(N_g)
estadd scalar N_x = matNg[1,1]
estimates store PrivElecREALMP
// Or social expenditure, generally
xtmelogit privelec sectorpub stinc eduyears age union prgal lrid lrid_x_prgal socexp_t_pmp lrid_x_socexp || ccode:
mat matNg = e(N_g)
estadd scalar N_x = matNg[1,1]
estimates store PrivElecRESocexp


// Base spec for the old argument
xtmelogit privelec sectorpub stinc eduyears age union wcoord union_x_wcoord || ccode:
mat matNg = e(N_g)
estadd scalar N_x = matNg[1,1]
estimates store PriveElecREWC
// Not robust to the inclusion of party id
xtmelogit privelec sectorpub stinc eduyears age union wcoord union_x_wcoord lrid || ccode:
mat matNg = e(N_g)
estadd scalar N_x = matNg[1,1]
estimates store PriveElecREWCLR
// Base spec for the old argument
jackknife, cluster(ccode): xtmelogit privelec sectorpub stinc eduyears age union wcoord union_x_wcoord lrid || ccode:
mat matNg = e(N_g)
estadd scalar N_x = matNg[1,1]
estimates store PriveElecREWCJack

// WCoord does not interact with LRId, but PR does - as expected.
xtmelogit privelec sectorpub stinc eduyears age union lrid  wcoord lrid_x_wcoord || ccode:
mat matNg = e(N_g)
estadd scalar N_x = matNg[1,1]
estimates store PriveElecRELRxWC
xtmelogit privelec sectorpub stinc eduyears age union lrid  wcoord lrid_x_wcoord prgal lrid_x_prgal || ccode:
mat matNg = e(N_g)
estadd scalar N_x = matNg[1,1]
estimates store PriveElecRELRxWCPR
// The PR interaction performs much better than the WC interaction with union, indicating that the effect runs through politics rather than union prompting.
xtmelogit privelec sectorpub stinc eduyears age union prgal wcoord union_x_prgal union_x_wcoord || ccode:
mat matNg = e(N_g)
estadd scalar N_x = matNg[1,1]
estimates store PriveElecRExUWC


xtmelogit privelec sectorpub stinc eduyears age union logmdmh  lrid  lrid_x_logmdmh   || ccode:
mat matNg = e(N_g)
estadd scalar N_x = matNg[1,1]
estimates store PriveElecREDM

xtmelogit privelec sectorpub stinc eduyears age union prrel lrid  lrid_x_prrel    || ccode:
mat matNg = e(N_g)
estadd scalar N_x = matNg[1,1]
estimates store PriveElecREPRRel

xtmelogit privelec sectorpub stinc eduyears age union prabso lrid  lrid_x_prabso   || ccode:
mat matNg = e(N_g)
estadd scalar N_x = matNg[1,1]
estimates store PriveElecREPRAbso

// Label variables with latex syntax so estout exports work nicely
label var ctry_Germany		"$ Germany $"
label var ctry_Norway		"$ Norway $"
label var ctry_Britain		"$ Britain $"
label var ctry_France		"$ France $"
label var ctry_Sweden		"$ Sweden $"
label var ctry_Italy		"$ Italy $"
label var ctry_Ireland		"$ Ireland $"
label var ctry_Spain		"$ Spain $"
label var union				"$ Union $"
label var sectorpub			"$ PubSector $"
label var sector_govt		"$ GovtSector $"
label var sector_soe		"$ SOESector $"
label var stinc				"$ Income $"
label var female			"$ Female $"
label var age				"$ Age $"
label var eduyears			"$ Education $"
label var pub_x_stinc		"$ PubSector \cdot Income $"
label var pub_x_female		"$ PubSector \cdot Female $"
label var govt_x_stinc		"$ GovtSector \cdot Income $"
label var govt_x_female		"$ GovtSector \cdot Female $"
label var soe_x_stinc		"$ SOESector \cdot Income $"
label var lrid				"$ IdLeft $"
label var lridb				"$ IdLeft^b $"
label var prgal				"$ PR $"
label var prrel				"$ PR^{Rel} $"
label var prabso			"$ PR^{Abs} $"
label var logmdmh			"$ \log(DistMag) $"
label var lrid_x_prgal		"$ IdLeft \cdot PR $"
label var lridb_x_prgal		"$ IdLeft^b \cdot PR $"
label var lrid_x_prrel		"$ IdLeft \cdot PR^{Rel} $"
label var lrid_x_prabs		"$ IdLeft \cdot PR^{Abs} $"
label var lrid_x_logmdmh	"$ IdLeft \cdot \log(DistMag) $"
label var lrid_x_wcoord		"$ IdLeft \cdot WageCoord $"
label var lridb_x_wcoord	"$ IdLeft^b \cdot WageCoord $"
label var singmemd			"$ SMD $"
label var lrid_x_singmemd	"$ IdLeft \cdot SMD $"
label var lridb_x_singmemd	"$ IdLeft^b \cdot SMD $"
label var effpar_leg		"$ Parties $"
label var lrid_x_effparleg	"$ IdLeft \cdot Parties $"
label var lridb_x_effparleg	"$ IdLeft^b \cdot Parties $"
label var socexp_t_pmp		"$ SocExp $"
label var lrid_x_socexp		"$ IdLeft \cdot SocExp $"
*label var lridb_x_socexp	"$ IdLeft^b \cdot SocExp $"
label var almp_pmp			"$ ALMP $"
label var lrid_x_almp		"$ IdLeft \cdot ALMP $"
*label var lridb_x_almp		"$ IdLeft^b \cdot ALMP $"
label var wcoord			"$ WageCoord $"
label var pub_x_wcoord		"$ PubSector \cdot WageCoord $"
label var union_x_wcoord	"$ Union \cdot WageCoord $"
label var pub_x_wcoord		"$ PubSector \cdot WageCoord $"
*label var union_x_uden		"$ Union \cdot UDen $"
label var union_x_prgal		"$ Union \cdot PR $"
label var pub_x_prgal		"$ PubSector \cdot PR $"


local out_dir "$repos/writing/Privatization/empirical"
local estout_options label replace style(tex) stats(N N_x chi2 ll, labels("N" "N countries" "$\chi^2$" "Log likelihood")) posthead("\hline") prefoot(\hline) postfoot("\hline") substitute("/t" "") eform

*estout  PrivElecRE1       PrivElecRE2        PrivElecRE2Jack	using "`out_dir'/models_prefs.tex", 		cells( " b( fmt(a2) ) se(fmt(3)) p(fmt(2)) " ) drop(_cons) `estout_options' order(sectorpub stinc eduyears age union prgal lrid) mlabels("(A1)" "(A2)" "(A3)")
*estout  PrivElecREEffPar  PrivElecRESocexp   PrivElecREALMP		using "`out_dir'/models_prefs_robust2.tex",	cells( " b( fmt(a2) ) se(fmt(3)) p(fmt(2)) " ) drop(_cons) `estout_options' order(sectorpub stinc eduyears age union prgal lrid) mlabels("(A4)" "(A5)" "(A6)")
*estout  PriveElecREWC     PriveElecREWCLR    PriveElecREWCJack	using "`out_dir'/models_prefs_robust.tex",	cells( " b( fmt(a2) ) se(fmt(3)) p(fmt(2)) " ) drop(_cons) `estout_options' order(sectorpub stinc eduyears age union lrid) mlabels("(A7)" "(A8)" "(A9)")
*estout  PriveElecRExUWC   PriveElecRELRxWC   PriveElecRELRxWCPR	using "`out_dir'/models_prefs_lrunion.tex",	cells( " b( fmt(a2) ) se(fmt(3)) p(fmt(2)) " ) drop(_cons) `estout_options' order(sectorpub stinc eduyears age union prgal lrid) mlabels("(A10)" "(A11)" "(A12)")
estout  PriveElecREDM     PriveElecREPRRel   PriveElecREPRAbso	using "`out_dir'/models_prefs_morepr.tex",	cells( " b( fmt(a2) ) se(fmt(3)) p(fmt(2)) " ) drop(_cons) `estout_options' order(sectorpub stinc eduyears age union lrid) mlabels("(A13)" "(A14)" "(A15)")


//// Plotting conditional effects

// Clear old data
drop _all
//do "`base_dir'\empirical\marginal_effects_plot_helper.do"
// Generate new variables
gen model = ""
* Unified
gen       clrid = .
label var clrid "Conditional Partisanship Effect"
gen       clrid_upper95 = .
label var clrid_upper95 "Upper bound (95%)"
gen       clrid_lower95 = .
label var clrid_lower95 "Lower bound (95%)"
* Conditioning variable
gen lambda = .
gen lambda_counter = .

local models PrivElecRE2

// Iterate over the stored models and fill up our data set
// Get ourselves the first (empty) observation
set obs 1
foreach model_name in `models' {
  estimates restore `model_name'
  replace model = "`model_name'" in l
  if "`model_name'" == "PrivElecRE2" {
    local conditional "lrid_x_prgal"
    local lambda_min    = round(75.39, 1)
    local lambda_max    = round(99.21, 1)
  }
  local lambda_mean    = (`lambda_max' - `lambda_min')/2
  local lambda_diff    = (`lambda_max' - `lambda_min')/10
  local counter = 1
  replace lambda_counter = `counter' in l
  forvalues intcount = 0/10 {
    local lambda_level = `lambda_min' + `intcount'*`lambda_diff'
    nlcom _b[lrid] + (_b[`conditional']*`lambda_level')
    local val el(r(b), 1, 1)
    local se  el(r(V), 1, 1)^(1/2)
    replace clrid = `val' if lambda_counter == `counter' & model == "`model_name'"
    replace clrid_upper95 = `val' + `se'*1.96 if lambda_counter == `counter' & model == "`model_name'"
    replace clrid_lower95 = `val' - `se'*1.96 if lambda_counter == `counter' & model == "`model_name'"
    replace lambda = `lambda_level' if lambda_counter == `counter' & model == "`model_name'"
    local counter = `counter' + 1
    local new = _N + 1
    set obs `new'
    replace model = "`model_name'" in l
	replace lambda_counter = `counter' in l
	}
}
// Get rid of the erroneously-added final observation.
drop in l

local models PrivElecRE2
foreach model_name in `models' {
  label var lambda  "Electoral proportionality"
  twoway (line clrid lambda, lpattern(solid) yline(0) lcolor(gs0)) (line clrid_upper95 lambda, yaxis(1) yscale(alt axis(1)) lpattern(dash) lcolor(gs0)) (line clrid_lower95 lambda, yaxis(1) yscale(alt axis(1)) lpattern(dash) lcolor(gs0)) if model == "`model_name'", legend(off) ytitle("LeftRightID Effect", axis(1)) xtitle("Electoral Proportionality") scheme(s1mono)
  graph export "$repos/writing/Privatization/images/conditional_effects_`model_name'.pdf", replace
*  graph export "$repos/writing/Privatization/images/conditional_effects_`model_name'.png", replace
}
window manage close graph
